{extend name="../../../view/public/base" /}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <form class="layui-form" lay-filter="addForm" autocomplete="off">
                    <!--目录-->
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">目录</label>
                        <div class="layui-input-block">
                            <input type="text" name="dir" placeholder="请输入目录" class="layui-input" value="{$row.dir}">
                        </div>
                    </div>
                    <!--名称-->
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" placeholder="请输入名称" class="layui-input" value="{$row.title}">
                        </div>
                    </div>
                    <!--描述内容-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">描述内容</label>
                        <div class="layui-input-block">
                            <textarea type="text" name="describe" placeholder="请输入描述内容" class="layui-input">{$row.describe|default =''}</textarea>
                        </div>
                    </div>
                    <!--Logo-->
                    <div class="layui-form-item">
                        <label class="layui-form-label required">Logo</label>
                        <div class="layui-input-block layuimini-upload">
                            <input name="logo_image" class="layui-input layui-col-xs6" lay-verify="required"  placeholder="请上传商品图片" value="{$row.logo_image|default=''}">
                            <div class="layuimini-upload-btn">
                                <span><a class="layui-btn" data-upload="logo_image" data-upload-number="one" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
                                <span><a class="layui-btn layui-btn-normal" id="select_logo_image" data-upload-select="logo_image" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span>
                            </div>
                        </div>
                    </div>
                    <!--演示二维码-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">演示二维码</label>
                        <div class="layui-input-block layuimini-upload">
                            <input name="qrcode_image" class="layui-input layui-col-xs6" lay-verify="required"  placeholder="请上传商品图片" value="{$row.qrcode_image|default=''}">
                            <div class="layuimini-upload-btn">
                                <span><a class="layui-btn" data-upload="qrcode_image" data-upload-number="one" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
                                <span><a class="layui-btn layui-btn-normal" id="select_qrcode_image" data-upload-select="qrcode_image" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span>
                            </div>
                        </div>
                    </div>
                    <!--应用截图-->

                    <div class="layui-form-item">
                        <label class="layui-form-label">应用截图</label>
                        <div class="layui-input-block layuimini-upload">
                            <input name="style_images" class="layui-input layui-col-xs6" lay-verify="required"  placeholder="请上传轮播图" value="{$row.style_images|default=''}">
                            <div class="layuimini-upload-btn">
                                <span><a class="layui-btn" data-upload="style_images" data-upload-number="more" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image" data-upload-sign=""><i class="fa fa-upload"></i> 上传</a></span>
                                <span><a class="layui-btn layui-btn-normal" id="select_style_images" data-upload-select="style_images" data-upload-number="more" data-upload-mimetype="image/*" data-upload-sign=""><i class="fa fa-list"></i> 选择</a></span>
                            </div>

                            <div></div>
                        </div>
                    </div>

                    <!--销售价-->
<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">销售价</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <input type="number" name="sell_price" placeholder="请输入销售价" class="layui-input" value="{$row.sell_price}">-->
<!--                        </div>-->
<!--                    </div>-->
                    <!--市场价-->
<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">市场价</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <input type="number" name="market_price" placeholder="请输入市场价" class="layui-input" value="{$row.market_price}">-->
<!--                        </div>-->
<!--                    </div>-->
                    <!--版本-->
<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">版本</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <input type="text" name="version" placeholder="请输入版本" class="layui-input" value="{$row.version}">-->
<!--                        </div>-->
<!--                    </div>-->
                    <!--小程序模板-->
<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">小程序模板</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <input type="number" name="template_id" placeholder="请输入小程序模板" class="layui-input" value="{$row.template_id}">-->
<!--                        </div>-->
<!--                    </div>-->
                    <!--体验天数-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">体验天数</label>
                        <div class="layui-input-block">
                            <input type="number" name="expire_day" placeholder="请输入体验天数" class="layui-input" value="{$row.expire_day}">
                        </div>
                    </div>
                    <!--详情介绍-->
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">详情介绍</label>
                        <div class="layui-input-block">
                            <script id="content" name="content" type="text/plain" style="width:100%;height:500px;"></script>
                            <script>
                                //实例化编辑器
                                UE.getEditor('content');
                                //格式化富文本
                                htmlDecode("{$row.content}", 'content')
                            </script>
                        </div>
                    </div>
                    <!--应用类型-->
                    <div class='layui-form-item'>
                        <label class='layui-form-label'>应用类型</label>
                        <div class='layui-input-block'>
                            <input type='checkbox' name='types_mini_program' lay-skin='primary' title='微信小程序' {if false !== strpos($row.types, 'mini_program')}checked{/if}>
                            <input type='checkbox' name='types_app' lay-skin='primary' title='APP' {if false !== strpos($row.types, 'app')}checked{/if}>
                            <input type='checkbox' name='types_h5' lay-skin='primary' title='h5' {if false !== strpos($row.types, 'h5')}checked{/if}>
                            <input type='checkbox' name='types_official' lay-skin='primary' title='微信公众号' {if false !== strpos($row.types, 'official')}checked{/if}>
                            <input type='checkbox' name='types_tt_program' lay-skin='primary' title='头条小程序' {if false !== strpos($row.types, 'tt_program')}checked{/if}>

<!--                            <input type='checkbox' name='types_system' lay-skin='primary' title='系统' {if false !== strpos($row.types, 'system')}checked{/if}>-->
                        </div>
                    </div>
                    <!--平台管理:0=关闭,1=开启-->
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">平台管理</label>
                            <div class="layui-input-block" id="is_manage">
                                <select name="is_manage" lay-verify="required">
                                    <option value="" >请选择平台管理</option>
                                    <option value="0" {eq name="$row.is_manage" value="0"}selected{/eq}>关闭</option>
                                    <option value="1" {eq name="$row.is_manage" value="1"}selected{/eq}>开启</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!--接入方式:0=独立应用,1=开放平台-->
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">接入方式</label>
                            <div class="layui-input-block" id="is_openapp">
                                <select name="is_openapp" lay-verify="required">
                                    <option value="" >请选择接入方式</option>
                                    <option value="0" {eq name="$row.is_openapp" value="0"}selected{/eq}>独立应用</option>
                                    <option value="1" {eq name="$row.is_openapp" value="1"}selected{/eq}>开放平台</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!--微信支付:0=关闭,1=开启-->
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">微信支付</label>
                            <div class="layui-input-block" id="is_wechat_pay">
                                <select name="is_wechat_pay" lay-verify="required">
                                    <option value="" >请选择微信支付</option>
                                    <option value="0" {eq name="$row.is_wechat_pay" value="0"}selected{/eq}>关闭</option>
                                    <option value="1" {eq name="$row.is_wechat_pay" value="1"}selected{/eq}>开启</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!--支付宝支付:0=关闭,1=开启-->
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">支付宝支付</label>
                            <div class="layui-input-block" id="is_alipay_pay">
                                <select name="is_alipay_pay" lay-verify="required">
                                    <option value="" >请选择支付宝支付</option>
                                    <option value="0" {eq name="$row.is_alipay_pay" value="0"}selected{/eq}>关闭</option>
                                    <option value="1" {eq name="$row.is_alipay_pay" value="1"}selected{/eq}>开启</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!--头条支付:0=关闭,1=开启-->
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">头条微信支付</label>
                            <div class="layui-input-block" id="is_tt_wechat_pay">
                                <select name="is_tt_wechat_pay" lay-verify="required">
                                    <option value="" >请选择头条微信支付</option>
                                    <option value="0" {eq name="$row.is_tt_wechat_pay" value="0"}selected{/eq}>关闭</option>
                                    <option value="1" {eq name="$row.is_tt_wechat_pay" value="1"}selected{/eq}>开启</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!--排序-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" placeholder="请输入排序" class="layui-input" value="{$row.sort}">
                        </div>
                    </div>

                    <!--定制应用-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">定制应用</label>
                        <div class="layui-input-block">
                            <input type="radio" name="is_diy" value="1" title="是" lay-filter="diy_member_ids" {eq name="$row.is_diy" value="1"}checked{/eq}>
                            <input type="radio" name="is_diy" value="0" title="否" lay-filter="diy_member_ids" {eq name="$row.is_diy" value="0"}checked{/eq}>
                        </div>
                    </div>

                    <!--定制租户id-->
                    <div class="layui-form-item" id="transfer_diy_member_ids">
                        <label class="layui-form-label">定制租户</label>
                        <div class="layui-input-block">
                            <div id="diy_member_ids" class="demo-transfer"></div>
                        </div>
                    </div>

                    <!--状态:-1=未安装,0=关闭,1=开启-->
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block" id="status">
                                <select name="status" lay-verify="required">
                                    <option value="" >请选择状态</option>
                                    <option value="-1" disabled {eq name="$row.status" value="-1"}selected{/eq}>未安装</option>
                                    <option value="0" {eq name="$row.status" value="0"}selected{/eq}>关闭</option>
                                    <option value="1" {eq name="$row.status" value="1"}selected{/eq}>开启</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">标签</label>
                        <div class="layui-input-inline">
                            <select name="label" >
                                <option value="" >无标签</option>
                                <option value="recommend" {eq name="$row.label" value="recommend"}selected{/eq}>推荐</option>
                                <option value="hot" {eq name="$row.label" value="hot"}selected{/eq}>热门</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submitBtn">立即提交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    var form, upload, laydate,bwimage,transfer;

    layui.use(['form', 'upload', 'laydate', 'transfer','bwimage'], function () {
        form = layui.form;
        upload = layui.upload;
        laydate = layui.laydate;
        transfer = layui.transfer;
        bwimage = layui.bwimage;
        bwimage.upload();
        //穿梭狂
        transfer.render({
            elem: '#diy_member_ids'
            ,id: 'diy_member_ids'
            ,title: ['租户列表', '可购买租户']
            ,showSearch: true
            ,height: 250
            ,data: JSON.parse('{$members|raw}')
            ,value: [{$row['diy_member_ids']}]
        });
        //初始是否需要渲染穿梭狂,不能在模板渲染时添加隐藏样式,会导致穿梭框样式错乱
        {$row.is_diy} == 0 && $('#transfer_diy_member_ids').addClass('layui-hide');
        //监听单选,决定是否渲染穿梭框
        form.on('radio(diy_member_ids)', function(data){
            if (data.value == 1) $('#transfer_diy_member_ids').removeClass('layui-hide');
            else $('#transfer_diy_member_ids').addClass('layui-hide');
        });

        // //Logo单图上传
        // bw_upload(upload, 'logo_image', 'image');
        // //演示二维码单图上传
        // bw_upload(upload, 'qrcode_image', 'image');
        // //应用截图多图上传
        // bw_upload(upload, 'style_images', 'images');
        //监听提交按钮
        form.on('submit(submitBtn)', function (obj) {
            data = obj.field;
            if(!data.dir) {layer.msg('目录不能为空', {icon: 2});return false}
            if(!data.title) {layer.msg('名称不能为空', {icon: 2});return false}
            if(!data.describe) {layer.msg('描述内容不能为空', {icon: 2});return false}
            if(!data.logo_image) {layer.msg('Logo不能为空', {icon: 2});return false}
            // if(!data.qrcode_image) {layer.msg('演示二维码不能为空', {icon: 2});return false}
            if(!data.style_images) {layer.msg('应用截图不能为空', {icon: 2});return false}
            // if(!data.sell_price) {layer.msg('销售价不能为空', {icon: 2});return false}
            // if(!data.market_price) {layer.msg('市场价不能为空', {icon: 2});return false}
            // if(!data.version) {layer.msg('版本不能为空', {icon: 2});return false}
            if(!data.expire_day) {layer.msg('体验天数不能为空', {icon: 2});return false}
            //应用类型 多选转为字符串
            var types = [];
            if(data.types_mini_program && data.types_mini_program == 'on') {
                types.push('mini_program');
                delete data.types_mini_program;
            }
            if(data.types_app && data.types_app == 'on') {
                types.push('app');
                delete data.types_app;
            }
            if(data.types_h5 && data.types_h5 == 'on') {
                types.push('h5');
                delete data.types_h5;
            }
            if(data.types_official && data.types_official == 'on') {
                types.push('official');
                delete data.types_official;
            }
            if(data.types_system && data.types_system == 'on') {
                types.push('system');
                delete data.types_system;
            }
            if(data.types_tt_program && data.types_tt_program == 'on') {
                types.push('tt_program');
                delete data.types_tt_program;
            }


            data.types = types.join(',');

            //定制租户
            if(data.is_diy == 0) data.diy_member_ids = '';
            else {
                var getData = transfer.getData('diy_member_ids');
                if(!getData.length) {layer.msg('请选择定制租户', {icon: 2});return false}

                var diy_member_ids = [];
                for (index in getData) {
                    diy_member_ids.push(getData[index].value);
                }
                data.diy_member_ids = ',' + diy_member_ids.join(',') + ',';
            }

            //ajax调用后台接口
            bwajax.post("/manage/admin/miniapp/edit?id={$row.id}", data)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg(response.data.msg, {icon: 1});
                        submitSuccess();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                })
            return false;
        })
    });
</script>
{/block}